﻿#include "DAVariantValueNodeGraphicsItem.h"
#include <QPainter>
#include "DANodePalette.h"
#include "DAAbstractNodeWidget.h"
#include "DAVariantValueNodeWidget.h"
DAVariantValueNodeGraphicsItem::DAVariantValueNodeGraphicsItem(DAVariantValueNode *n, QGraphicsItem *p)
    : DAStandardNodeGraphicsItem(n, p)
{
    DAAbstractNodeWidget *w = new DAVariantValueNodeWidget(this);

    setNodeWidget(w);
}

DAVariantValueNodeGraphicsItem::~DAVariantValueNodeGraphicsItem() {}

/**
 * @brief 设置参数，如果无法设置成功返回false
 *
 * 支持 int double string datetime color pointf
 * @param v
 * @return
 */
bool DAVariantValueNodeGraphicsItem::setValue(const QVariant &v)
{
    switch (v.type()) {
    case QVariant::Double:
    case QVariant::String:
    case QVariant::DateTime:
    case QVariant::PointF:
    case QVariant::Color:
        m_value = v;
        return (true);

    case QVariant::Int:
    case QVariant::UInt:
    case QVariant::ULongLong:
    case QVariant::LongLong:
        m_value = v.toInt();
        return (true);

    case QVariant::Point: // Point也转换为PointF
        m_value = QPointF(v.toPoint());
        return (true);

    default:
        break;
    }
    return (false);
}

QVariant DAVariantValueNodeGraphicsItem::getValue() const
{
    return (m_value);
}

bool DAVariantValueNodeGraphicsItem::isNull() const
{
    bool b = m_value.isValid();

    b &= m_value.isNull();
    b &= checkType();
    return (b);
}

bool DAVariantValueNodeGraphicsItem::checkType() const
{
    switch (m_value.type()) {
    case QVariant::Double:
    case QVariant::String:
    case QVariant::DateTime:
    case QVariant::PointF:
    case QVariant::Color:
    case QVariant::Int:
        return (true);

    default:
        break;
    }
    return (false);
}
